app.controller('ItemCtrl', function ($scope, ajax, $modal, toaster, page, authorities) {
	//根据查询返回的列表数据
	$scope.itemList = [];

	$scope.noAuth = !authorities.hasAuthority('SYSTEM_NOTIFY_ITEM_UPDATE');
	$scope.module = [
		{ id: 1, name: '订票' },
		{ id: 2, name: '城际约租' },
		{ id: 3, name: '跨城巴士' }
	];
	$scope.rest = {
		page: {
			uri: 'notify/item/{pageNumber}/{pageSize}/page',
			res: function (pageNumber) {
				return this.uri.format({ pageNumber: pageNumber, pageSize: $scope.maxSize });
			},
			callback: function (data) {
				if (data && data.code == 200) {
					$scope.bigTotalItems = data.totalCount;
					$scope.bigCurrentPage = data.pageNumber;
					$scope.itemList = data.result;
				} else {
					$scope.itemList = [];
				}
			}
		},
		sms: {
			uri: 'notify/item/{id}/sms',
			res: function (id) { return this.uri.format({ id: id }); },
		},
		wx: {
			uri: 'notify/item/{id}/wx',
			res: function (id) { return this.uri.format({ id: id }); },
		},
		jpush: {
			uri: 'notify/item/{id}/jpush',
			res: function (id) { return this.uri.format({ id: id }); },
		},
		switch: {
			uri: 'notify/item/{id}/switch',
			res: function (id) { return this.uri.format({ id: id }); },
		},
		tpl: {
			uri: 'notify/tpl/update',
			res: function () { return this.uri; },
		}
	};

	page.index($scope, $scope.rest.page);
	page.load(1, $scope.rest.page);

	//模糊查询
	$scope.queryMap = {};
	$scope.query = function () {
		if ($scope.queryMap) {
			if ($scope.module.selected) {
				$scope.queryMap.moduleId = $scope.module.selected.id;
			} else {
				delete $scope.queryMap.moduleId;
			}
			page.load(new Req.GET({
				page_index: 1,
				params: $scope.queryMap,
				resource: $scope.rest.page.res(1)
			}), $scope.rest.page);
		} else {
			page.load(1, $scope.rest.page);
		}
	};

	////////弹框/////////
	$scope.tpl = function (current_data) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/system/notifyItem/tplModal.html?v=c1979bf25e',
			controller: 'TemplateModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return { "data": $scope.current_data.notifyTemplate };
				}
			}
		}).result.then(function (param) {
			ajax.put($scope.rest.tpl.res(), { data: param.data }, function (data) {
				if (data && data.code == 200) {
					current_data.notifyTemplate = param.data;
					toaster.pop("success", "成功", "修改成功。");
				} else {
					toaster.pop("error", "错误", data.result);
				}
			}, function () {
				toaster.pop("error", "错误", "修改失败。");
			});
		});
	};

	$scope.sms = function (item) {
		if ($scope.noAuth) return false;
		ajax.call(new Req.POST({ resource: $scope.rest.sms.res(item.id) }))
			.then(function (result) {
				if (result && result.code == 200) {
					item.sms = !item.sms;
					return true;
				} else {
					toaster.pop('error', '错误', result.result);
					return false;
				}
			}, function () {
				toaster.pop('error', '错误', '修改失败！');
				return false;
			});
	};
	$scope.wx = function (item) {
		if ($scope.noAuth) return false;
		ajax.call(new Req.POST({ resource: $scope.rest.wx.res(item.id) }))
			.then(function (result) {
				if (result && result.code == 200) {
					item.wx = !item.wx;
					return true;
				} else {
					toaster.pop('error', '错误', result.result);
					return false;
				}
			}, function () {
				toaster.pop('error', '错误', '修改失败！');
				return false;
			});
	};
	$scope.jpush = function (item) {
		if ($scope.noAuth) return false;
		ajax.call(new Req.POST({ resource: $scope.rest.jpush.res(item.id) }))
			.then(function (result) {
				if (result.code == 200) {
					item.jpush = !item.jpush;
					return true;
				} else {
					toaster.pop('error', '错误', result.result);
					return false;
				}
			}, function () {
				toaster.pop('error', '错误', '修改失败！');
				return false;
			});
	};

	$scope.switch = function (item) {
		if ($scope.noAuth) return false;
		ajax.call(new Req.POST({ resource: $scope.rest.switch.res(item.id) }))
			.then(function (result) {
				if (result.code == 200) {
					item.enabled = !item.enabled;
					return true;
				} else {
					toaster.pop('error', '错误', result.result);
					return false;
				}
			}, function () {
				toaster.pop('error', '错误', '修改失败！');
				return false;
			});
	};
});
